import json
from datetime import datetime
from dji_redis.redis_client import RedisClient
from mqtt.callback.callback_manager import callbackManager
from mqtt.method.control_fly_method import ControlFlyMethodEnum
from business.business_constant import BusinessConstant

redis_client = RedisClient()
class EventCallbackHandler(callbackManager):
    def handle(self, payload):
        load_data = json.loads(payload)
        method = load_data['method']
        data = load_data['data']
        now = datetime.now()
        log_time = now.strftime('%Y%m%d %H%M%S')
        if ControlFlyMethodEnum.FLY_TO_POINT_PROGRESS.get_method() == method :
            try:
                fly_to_id = data['fly_to_id']
                redis_key = BusinessConstant.FLY_KEY % fly_to_id
                redis_client.set(redis_key, data['status'])
                print(f"fly_to_id={fly_to_id}: {data['status']}")
                self.publish_callback_message(f"{log_time}_接收指点飞行状态回调:{payload}")
            except Exception as e:
                print(str(e))



